{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Testing DBSCAN encapsulation code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from GeoDBSCAN import *\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Evaluation criteria, for testing, not the final modular function block\n",
    "from sklearn import metrics\n",
    "import numpy as np\n",
    "def get_marks(estimator, data, name=None, kmeans=None, af=None,db=None):\n",
    "    \"\"\"To get the score, there are five kinds of actual classification information that are required to know the data set, 
    and there are three kinds that are not required, refer to the readme.txt\n",
    "    \n",
    "    :param estimator: Model\n",
    "    :param name: Orginal method\n",
    "    :param data: feature data set\n",
    "    \"\"\"\n",
    "    estimator.fit(data.astype(np.float64))\n",
    "    print(30 * '*', name, 30 * '*')\n",
    "    print(\"Model and parameters      : \", estimator )\n",
    "    print(\"Homogeneity Score         : \", metrics.homogeneity_score(labels, estimator.labels_))\n",
    "    print(\"Completeness Score        : \", metrics.completeness_score(labels, estimator.labels_))\n",
    "    print(\"V-Measure Score           : \", metrics.v_measure_score(labels, estimator.labels_))\n",
    "    print(\"Adjusted Rand Score       : \", metrics.adjusted_rand_score(labels, estimator.labels_))\n",
    "    print(\"Adjusted Mutual Info Score: \", metrics.adjusted_mutual_info_score(labels, estimator.labels_))\n",
    "    print(\"Calinski Harabasz Score   :   \", metrics.calinski_harabasz_score(data, estimator.labels_))\n",
    "    print(\"Silhouette Score          : \", metrics.silhouette_score(data, estimator.labels_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>TRUE VALUE</th>\n",
       "      <th>Si(wt%)</th>\n",
       "      <th>Ti(wt%)</th>\n",
       "      <th>Al(wt%)</th>\n",
       "      <th>Cr(wt%)</th>\n",
       "      <th>Fe(wt%)</th>\n",
       "      <th>Mn(wt%)</th>\n",
       "      <th>Mg(wt%)</th>\n",
       "      <th>Ca(wt%)</th>\n",
       "      <th>O(wt%)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3</td>\n",
       "      <td>19.212804</td>\n",
       "      <td>1.798200e-03</td>\n",
       "      <td>4.946714e-03</td>\n",
       "      <td>0.014368</td>\n",
       "      <td>5.402782</td>\n",
       "      <td>0.075899</td>\n",
       "      <td>30.873551</td>\n",
       "      <td>0.011200</td>\n",
       "      <td>44.205834</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>19.177804</td>\n",
       "      <td>5.994000e-07</td>\n",
       "      <td>3.176400e-03</td>\n",
       "      <td>0.016421</td>\n",
       "      <td>5.263226</td>\n",
       "      <td>0.073302</td>\n",
       "      <td>30.845400</td>\n",
       "      <td>0.010500</td>\n",
       "      <td>44.101122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>19.380805</td>\n",
       "      <td>5.994000e-07</td>\n",
       "      <td>6.352800e-03</td>\n",
       "      <td>0.014368</td>\n",
       "      <td>5.157615</td>\n",
       "      <td>0.061308</td>\n",
       "      <td>31.394866</td>\n",
       "      <td>0.022143</td>\n",
       "      <td>44.670443</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>19.403532</td>\n",
       "      <td>5.994000e-07</td>\n",
       "      <td>3.176400e-03</td>\n",
       "      <td>0.015052</td>\n",
       "      <td>4.866336</td>\n",
       "      <td>0.068147</td>\n",
       "      <td>31.178520</td>\n",
       "      <td>0.005714</td>\n",
       "      <td>44.461371</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>19.320138</td>\n",
       "      <td>4.795200e-02</td>\n",
       "      <td>9.952720e+00</td>\n",
       "      <td>4.987818</td>\n",
       "      <td>5.008903</td>\n",
       "      <td>0.232320</td>\n",
       "      <td>13.140000</td>\n",
       "      <td>2.342904</td>\n",
       "      <td>44.537336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>967</th>\n",
       "      <td>2</td>\n",
       "      <td>19.884809</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>9.185090e+00</td>\n",
       "      <td>4.187304</td>\n",
       "      <td>5.810017</td>\n",
       "      <td>0.255552</td>\n",
       "      <td>13.296000</td>\n",
       "      <td>2.414334</td>\n",
       "      <td>44.386100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>968</th>\n",
       "      <td>3</td>\n",
       "      <td>19.301565</td>\n",
       "      <td>2.997000e-03</td>\n",
       "      <td>1.689845e-02</td>\n",
       "      <td>0.050631</td>\n",
       "      <td>6.042573</td>\n",
       "      <td>0.085184</td>\n",
       "      <td>30.660840</td>\n",
       "      <td>0.057144</td>\n",
       "      <td>44.397332</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>969</th>\n",
       "      <td>3</td>\n",
       "      <td>19.348885</td>\n",
       "      <td>2.997000e-03</td>\n",
       "      <td>5.294000e-07</td>\n",
       "      <td>0.045841</td>\n",
       "      <td>5.337126</td>\n",
       "      <td>0.071245</td>\n",
       "      <td>31.104720</td>\n",
       "      <td>0.012143</td>\n",
       "      <td>44.509132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>970</th>\n",
       "      <td>2</td>\n",
       "      <td>19.446139</td>\n",
       "      <td>5.994000e-01</td>\n",
       "      <td>9.529200e+00</td>\n",
       "      <td>3.421000</td>\n",
       "      <td>5.444460</td>\n",
       "      <td>0.619520</td>\n",
       "      <td>12.000000</td>\n",
       "      <td>4.285800</td>\n",
       "      <td>44.123161</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>971</th>\n",
       "      <td>4</td>\n",
       "      <td>26.320188</td>\n",
       "      <td>3.596400e-02</td>\n",
       "      <td>2.911700e-01</td>\n",
       "      <td>0.041052</td>\n",
       "      <td>8.835581</td>\n",
       "      <td>0.139392</td>\n",
       "      <td>18.600000</td>\n",
       "      <td>0.835731</td>\n",
       "      <td>45.726476</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>972 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     TRUE VALUE    Si(wt%)       Ti(wt%)       Al(wt%)   Cr(wt%)   Fe(wt%)  \\\n",
       "0             3  19.212804  1.798200e-03  4.946714e-03  0.014368  5.402782   \n",
       "1             3  19.177804  5.994000e-07  3.176400e-03  0.016421  5.263226   \n",
       "2             3  19.380805  5.994000e-07  6.352800e-03  0.014368  5.157615   \n",
       "3             3  19.403532  5.994000e-07  3.176400e-03  0.015052  4.866336   \n",
       "4             2  19.320138  4.795200e-02  9.952720e+00  4.987818  5.008903   \n",
       "..          ...        ...           ...           ...       ...       ...   \n",
       "967           2  19.884809  0.000000e+00  9.185090e+00  4.187304  5.810017   \n",
       "968           3  19.301565  2.997000e-03  1.689845e-02  0.050631  6.042573   \n",
       "969           3  19.348885  2.997000e-03  5.294000e-07  0.045841  5.337126   \n",
       "970           2  19.446139  5.994000e-01  9.529200e+00  3.421000  5.444460   \n",
       "971           4  26.320188  3.596400e-02  2.911700e-01  0.041052  8.835581   \n",
       "\n",
       "      Mn(wt%)    Mg(wt%)   Ca(wt%)     O(wt%)  \n",
       "0    0.075899  30.873551  0.011200  44.205834  \n",
       "1    0.073302  30.845400  0.010500  44.101122  \n",
       "2    0.061308  31.394866  0.022143  44.670443  \n",
       "3    0.068147  31.178520  0.005714  44.461371  \n",
       "4    0.232320  13.140000  2.342904  44.537336  \n",
       "..        ...        ...       ...        ...  \n",
       "967  0.255552  13.296000  2.414334  44.386100  \n",
       "968  0.085184  30.660840  0.057144  44.397332  \n",
       "969  0.071245  31.104720  0.012143  44.509132  \n",
       "970  0.619520  12.000000  4.285800  44.123161  \n",
       "971  0.139392  18.600000  0.835731  45.726476  \n",
       "\n",
       "[972 rows x 10 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_excel('test4.xlsx')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = df.drop('TRUE VALUE', axis = 1)\n",
    "labels = df['TRUE VALUE']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test grid search\n",
    "eps = np.linspace(1,7,5)\n",
    "min_samples = list(range(3,7))\n",
    "dbscan = GeoDBSCAN()\n",
    "dbscan_dict = {'eps': eps,\n",
    "        'min_samples': min_samples,\n",
    "        'metric':['cityblock','euclidean']}\n",
    "dbscan.DBSCANGridsearch(data,dbscan_dict,labels,measure = 'AMIC', best = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****************************** output0 ******************************\n",
      "Model and parameters :  DBSCAN(eps=1.0, metric='cityblock', min_samples=3)\n",
      "Homogeneity Score        :  0.6665852932349084\n",
      "Completeness Score        :  0.5707944113579064\n",
      "V-Measure Score           :  0.6149820603321803\n",
      "Adjusted Rand Score       :  0.6536883805584567\n",
      "Adjusted Mutual Info Score:  0.6056969783775303\n",
      "Calinski Harabasz Score:    76.32521025517052\n",
      "Silhouette Score          :  0.10334820792118159\n",
      "****************************** output1 ******************************\n",
      "Model and parameters :  DBSCAN(eps=1.0, metric='cityblock', min_samples=4)\n",
      "Homogeneity Score         :  0.6400552497487421\n",
      "Completeness Score        :  0.6119695120772671\n",
      "V-Measure Score           :  0.6256973677101505\n",
      "Adjusted Rand Score       :  0.6526290218633497\n",
      "Adjusted Mutual Info Score:  0.6197740995431089\n",
      "Calinski Harabasz Score:    116.72586697383781\n",
      "Silhouette Score          :  0.16620329233060083\n",
      "****************************** output2 ******************************\n",
      "Model and parameters :  DBSCAN(eps=1.0, metric='cityblock')\n",
      "Homogeneity Score         :  0.635375447597385\n",
      "Completeness Score        :  0.6213388216002427\n",
      "V-Measure Score           :  0.628278744914614\n",
      "Adjusted Rand Score       :  0.6570229629194912\n",
      "Adjusted Mutual Info Score:  0.6223067776086577\n",
      "Calinski Harabasz Score:   114.95653074912151\n",
      "Silhouette Score          :  0.14898329711367125\n",
      "****************************** output3 ******************************\n",
      "Model and parameters :  DBSCAN(eps=1.0, metric='cityblock', min_samples=6)\n",
      "Homogeneity Score         :  0.6195833845560237\n",
      "Completeness Score        :  0.6222258483900466\n",
      "V-Measure Score           :  0.6209018050045225\n",
      "Adjusted Rand Score      :  0.6481738649048312\n",
      "Adjusted Mutual Info Score:  0.614722186894708\n",
      "Calinski Harabasz Score:    109.74186274329749\n",
      "Silhouette Score          :  0.1280031070294141\n",
      "****************************** output4 ******************************\n",
      "Model and parameters :  DBSCAN(eps=1.0, min_samples=3)\n",
      "Homogeneity Score        :  0.8395090830876993\n",
      "Completeness Score       :  0.5121812606360037\n",
      "V-Measure Score           :  0.6362120177713205\n",
      "Adjusted Rand Score       :  0.6666095769036804\n",
      "Adjusted Mutual Info Score:  0.6250336218701245\n",
      "Calinski Harabasz Score:    77.38291783298779\n",
      "Silhouette Score          :  0.3210647228737985\n",
      "****************************** output5 ******************************\n",
      "Model and parameters :  DBSCAN(eps=1.0, min_samples=4)\n",
      "Homogeneity Score         :  0.8075954492491668\n",
      "Completeness Score        :  0.54786831472925\n",
      "V-Measure Score          :  0.6528480797811986\n",
      "Adjusted Rand Score       :  0.6683792728186074\n",
      "Adjusted Mutual Info Score:  0.6459045277032485\n",
      "Calinski Harabasz Score:    115.78004619710043\n",
      "Silhouette Score         :  0.2915523560510414\n",
      "****************************** output6 ******************************\n",
      "Model and parameters :  DBSCAN(eps=1.0)\n",
      "Homogeneity Score         :  0.7729915713976638\n",
      "Completeness Score        :  0.5737504929556823\n",
      "V-Measure Score           :  0.6586328694692563\n",
      "Adjusted Rand Score       :  0.6673089059441597\n",
      "Adjusted Mutual Info Score:  0.6540046366538504\n",
      "Calinski Harabasz Score:   162.9284988051468\n",
      "Silhouette Score          :  0.4219889870657859\n",
      "****************************** output7 ******************************\n",
      "Model and parameters:  DBSCAN(eps=1.0, min_samples=6)\n",
      "Homogeneity Score         :  0.7709518856299097\n",
      "Completeness Score        :  0.580443500371679\n",
      "V-Measure Score           :  0.662269555969381\n",
      "Adjusted Rand Score       :  0.6690019478406036\n",
      "Adjusted Mutual Info Score:  0.658074741458529\n",
      "Calinski Harabasz Score:   178.0852049768116\n",
      "Silhouette Score          :  0.4183748511934443\n",
      "****************************** output8 ******************************\n",
      "Model and parameters :  DBSCAN(eps=2.5, metric='cityblock', min_samples=3)\n",
      "Homogeneity Score         :  0.8606338802604118\n",
      "Completeness Score        :  0.5649127832336499\n",
      "V-Measure Score           :  0.6821005486434717\n",
      "Adjusted Rand Score       :  0.7101579556301236\n",
      "Adjusted Mutual Info Score:  0.6752369283397563\n",
      "Calinski Harabasz Score:   123.3617547689799\n",
      "Silhouette Score          :  0.4217839030244147\n",
      "****************************** output9 ******************************\n",
      "Model and parameters :  DBSCAN(eps=2.5, metric='cityblock', min_samples=4)\n",
      "Homogeneity Score         :  0.8488086183891962\n",
      "Completeness Score        :  0.5678182651834748\n",
      "V-Measure Score           :  0.6804459844091489\n",
      "Adjusted Rand Score       :  0.7029363780422548\n",
      "Adjusted Mutual Info Score:  0.6749026876445707\n",
      "Calinski Harabasz Score:   151.52587980166916\n",
      "Silhouette Score          :  0.46404379510699534\n",
      "****************************** output10 ******************************\n",
      "Model and parameters:  DBSCAN(eps=2.5, metric='cityblock')\n",
      "Homogeneity Score         :  0.8238816604616696\n",
      "Completeness Score        :  0.5563410055934103\n",
      "V-Measure Score           :  0.6641814581719415\n",
      "Adjusted Rand Score       :  0.6867544208858914\n",
      "Adjusted Mutual Info Score:  0.6583118096071107\n",
      "Calinski Harabasz Score:   141.3839915224019\n",
      "Silhouette Score          :  0.4282479218743061\n",
      "****************************** output11 ******************************\n",
      "Model and parameters:  DBSCAN(eps=2.5, metric='cityblock', min_samples=6)\n",
      "Homogeneity Score         :  0.8057723689885286\n",
      "Completeness Score        :  0.5820374292086078\n",
      "V-Measure Score           :  0.6758702507831604\n",
      "Adjusted Rand Score       :  0.6874468204060366\n",
      "Adjusted Mutual Info Score:  0.671960619880242\n",
      "Calinski Harabasz Score:   199.8143082136681\n",
      "Silhouette Score          :  0.4330514968588073\n",
      "****************************** output12 ******************************\n",
      "Model and parameters:  DBSCAN(eps=2.5, min_samples=3)\n",
      "Homogeneity Score         :  0.9216971798395237\n",
      "Completeness Score        :  0.7298044197564285\n",
      "V-Measure Score           :  0.8146025116639176\n",
      "Adjusted Rand Score       :  0.8785281074617433\n",
      "Adjusted Mutual Info Score:  0.8120606069652659\n",
      "Calinski Harabasz Score:   306.9221054875102\n",
      "Silhouette Score          :  0.4224640208579862\n",
      "****************************** output13 ******************************\n",
      "Model and parameters:  DBSCAN(eps=2.5, min_samples=4)\n",
      "Homogeneity Score         :  0.9135886633298024\n",
      "Completeness Score        :  0.7431856867772128\n",
      "V-Measure Score           :  0.8196240099260095\n",
      "Adjusted Rand Score       :  0.8762677673623266\n",
      "Adjusted Mutual Info Score:  0.8177960773734554\n",
      "Calinski Harabasz Score:   392.018245029816\n",
      "Silhouette Score          :  0.5871524863044474\n",
      "****************************** output14 ******************************\n",
      "Model and parameters:  DBSCAN(eps=2.5)\n",
      "Homogeneity Score         :  0.9050848106068775\n",
      "Completeness Score        :  0.7447036979957748\n",
      "V-Measure Score           :  0.8170986789447728\n",
      "Adjusted Rand Score       :  0.875052989619342\n",
      "Adjusted Mutual Info Score:  0.8154722771400243\n",
      "Calinski Harabasz Score:   438.6864257026745\n",
      "Silhouette Score          :  0.5883947572657355\n",
      "****************************** output15 ******************************\n",
      "Model and parameters:  DBSCAN(eps=2.5, min_samples=6)\n",
      "Homogeneity Score         :  0.9007000586263462\n",
      "Completeness Score        :  0.7228734950649798\n",
      "V-Measure Score           :  0.8020482939059314\n",
      "Adjusted Rand Score       :  0.8648136349505585\n",
      "Adjusted Mutual Info Score:  0.8003169713251416\n",
      "Calinski Harabasz Score:   428.0914972471942\n",
      "Silhouette Score          :  0.573078712022838\n",
      "****************************** output16 ******************************\n",
      "Model and parameters:  DBSCAN(eps=4.0, metric='cityblock', min_samples=3)\n",
      "Homogeneity Score         :  0.9098236654871856\n",
      "Completeness Score        :  0.6178194017468178\n",
      "V-Measure Score           :  0.7359136761235164\n",
      "Adjusted Rand Score       :  0.7608567372002663\n",
      "Adjusted Mutual Info Score:  0.7317392598263592\n",
      "Calinski Harabasz Score:   232.19070907355467\n",
      "Silhouette Score          :  0.5980021330625424\n",
      "****************************** output17 ******************************\n",
      "Model and parameters:  DBSCAN(eps=4.0, metric='cityblock', min_samples=4)\n",
      "Homogeneity Score         :  0.8960333624651953\n",
      "Completeness Score        :  0.6398317483552062\n",
      "V-Measure Score           :  0.7465637299156552\n",
      "Adjusted Rand Score       :  0.759409586210363\n",
      "Adjusted Mutual Info Score:  0.7442117664286385\n",
      "Calinski Harabasz Score:   326.51240834760597\n",
      "Silhouette Score          :  0.6199479046191274\n",
      "****************************** output18 ******************************\n",
      "Model and parameters:  DBSCAN(eps=4.0, metric='cityblock')\n",
      "Homogeneity Score         :  0.8825180072535342\n",
      "Completeness Score        :  0.6396285458403262\n",
      "V-Measure Score           :  0.7416942981083278\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adjusted Rand Score       :  0.7560663865175196\n",
      "Adjusted Mutual Info Score:  0.7395856611188443\n",
      "Calinski Harabasz Score:   360.0602578782702\n",
      "Silhouette Score          :  0.6146978274199002\n",
      "****************************** output19 ******************************\n",
      "Model and parameters:  DBSCAN(eps=4.0, metric='cityblock', min_samples=6)\n",
      "Homogeneity Score         :  0.8814871984626833\n",
      "Completeness Score        :  0.6390421217763994\n",
      "V-Measure Score           :  0.7409359913372203\n",
      "Adjusted Rand Score       :  0.7552602871378065\n",
      "Adjusted Mutual Info Score:  0.7388208072722882\n",
      "Calinski Harabasz Score:   358.3955781877382\n",
      "Silhouette Score          :  0.6127816950875717\n",
      "****************************** output20 ******************************\n",
      "Model and parameters:  DBSCAN(eps=4.0, min_samples=3)\n",
      "Homogeneity Score         :  0.9427213820741136\n",
      "Completeness Score        :  0.7915428790498983\n",
      "V-Measure Score           :  0.8605428983760675\n",
      "Adjusted Rand Score       :  0.919921868860641\n",
      "Adjusted Mutual Info Score:  0.8589314603111428\n",
      "Calinski Harabasz Score:   430.582603918571\n",
      "Silhouette Score          :  0.6180418866500216\n",
      "****************************** output21 ******************************\n",
      "Model and parameters:  DBSCAN(eps=4.0, min_samples=4)\n",
      "Homogeneity Score         :  0.937012852318852\n",
      "Completeness Score        :  0.8050738428791314\n",
      "V-Measure Score           :  0.86604706863655\n",
      "Adjusted Rand Score       :  0.91727810224893\n",
      "Adjusted Mutual Info Score:  0.8650070751567023\n",
      "Calinski Harabasz Score:   624.0787278689478\n",
      "Silhouette Score          :  0.6662052248892947\n",
      "****************************** output22 ******************************\n",
      "Model and parameters:  DBSCAN(eps=4.0)\n",
      "Homogeneity Score         :  0.937012852318852\n",
      "Completeness Score        :  0.8050738428791314\n",
      "V-Measure Score           :  0.86604706863655\n",
      "Adjusted Rand Score       :  0.91727810224893\n",
      "Adjusted Mutual Info Score:  0.8650070751567023\n",
      "Calinski Harabasz Score:   624.0787278689478\n",
      "Silhouette Score          :  0.6662052248892947\n",
      "****************************** output23 ******************************\n",
      "Model and parameters:  DBSCAN(eps=4.0, min_samples=6)\n",
      "Homogeneity Score         :  0.937012852318852\n",
      "Completeness Score        :  0.8050738428791314\n",
      "V-Measure Score           :  0.86604706863655\n",
      "Adjusted Rand Score       :  0.91727810224893\n",
      "Adjusted Mutual Info Score:  0.8650070751567023\n",
      "Calinski Harabasz Score:   624.0787278689478\n",
      "Silhouette Score          :  0.6662052248892947\n",
      "****************************** output24 ******************************\n",
      "Model and parameters:  DBSCAN(eps=5.5, metric='cityblock', min_samples=3)\n",
      "Homogeneity Score         :  0.9263466267193383\n",
      "Completeness Score        :  0.7305398183451071\n",
      "V-Measure Score           :  0.8168732364538431\n",
      "Adjusted Rand Score       :  0.8806810105333309\n",
      "Adjusted Mutual Info Score:  0.8143572430751229\n",
      "Calinski Harabasz Score:   345.8088495232172\n",
      "Silhouette Score          :  0.5135296020996627\n",
      "****************************** output25 ******************************\n",
      "Model and parameters:  DBSCAN(eps=5.5, metric='cityblock', min_samples=4)\n",
      "Homogeneity Score         :  0.9202333551561409\n",
      "Completeness Score        :  0.740088461799002\n",
      "V-Measure Score           :  0.8203880493031468\n",
      "Adjusted Rand Score       :  0.8794704180451604\n",
      "Adjusted Mutual Info Score:  0.8183449808203768\n",
      "Calinski Harabasz Score:   370.22588400879926\n",
      "Silhouette Score          :  0.5632803656402078\n",
      "****************************** output26 ******************************\n",
      "Model and parameters:  DBSCAN(eps=5.5, metric='cityblock')\n",
      "Homogeneity Score         :  0.9069573657298527\n",
      "Completeness Score        :  0.7450637054306208\n",
      "V-Measure Score           :  0.8180779620487527\n",
      "Adjusted Rand Score       :  0.8763573768342248\n",
      "Adjusted Mutual Info Score:  0.8164596109684893\n",
      "Calinski Harabasz Score:   462.3440947801006\n",
      "Silhouette Score          :  0.5925354081119686\n",
      "****************************** output27 ******************************\n",
      "Model and parameters:  DBSCAN(eps=5.5, metric='cityblock', min_samples=6)\n",
      "Homogeneity Score         :  0.9021544222693565\n",
      "Completeness Score        :  0.7537125810352313\n",
      "V-Measure Score           :  0.8212798935469925\n",
      "Adjusted Rand Score       :  0.8759618757066175\n",
      "Adjusted Mutual Info Score:  0.8199159997899979\n",
      "Calinski Harabasz Score:   510.72910794251186\n",
      "Silhouette Score          :  0.5892492988502928\n",
      "****************************** output28 ******************************\n",
      "Model and parameters:  DBSCAN(eps=5.5, min_samples=3)\n",
      "Homogeneity Score         :  0.9517134178901743\n",
      "Completeness Score        :  0.7993119369319421\n",
      "V-Measure Score           :  0.8688805029156118\n",
      "Adjusted Rand Score       :  0.9257305742036849\n",
      "Adjusted Mutual Info Score:  0.8673515112274802\n",
      "Calinski Harabasz Score:   491.69080982923333\n",
      "Silhouette Score          :  0.6621176190435124\n",
      "****************************** output29 ******************************\n",
      "Model and parameters:  DBSCAN(eps=5.5, min_samples=4)\n",
      "Homogeneity Score         :  0.9474022055919356\n",
      "Completeness Score        :  0.80708523435725\n",
      "V-Measure Score           :  0.8716327215803706\n",
      "Adjusted Rand Score       :  0.9256184287146857\n",
      "Adjusted Mutual Info Score:  0.8704568256685855\n",
      "Calinski Harabasz Score:   560.8592456371019\n",
      "Silhouette Score          :  0.6692276916095948\n",
      "****************************** output30 ******************************\n",
      "Model and parameters:  DBSCAN(eps=5.5)\n",
      "Homogeneity Score         :  0.9462415178618495\n",
      "Completeness Score        :  0.8075908455669056\n",
      "V-Measure Score           :  0.8714356097598673\n",
      "Adjusted Rand Score       :  0.9255469859093124\n",
      "Adjusted Mutual Info Score:  0.8702582848925636\n",
      "Calinski Harabasz Score:   555.022299066557\n",
      "Silhouette Score          :  0.6673179249914962\n",
      "****************************** output31 ******************************\n",
      "Model and parameters:  DBSCAN(eps=5.5, min_samples=6)\n",
      "Homogeneity Score         :  0.9424323150750386\n",
      "Completeness Score        :  0.8165710512386264\n",
      "V-Measure Score           :  0.8749988328389\n",
      "Adjusted Rand Score       :  0.9256505924643731\n",
      "Adjusted Mutual Info Score:  0.8740224643320074\n",
      "Calinski Harabasz Score:   625.6514299229482\n",
      "Silhouette Score          :  0.6669920144486613\n",
      "****************************** output32 ******************************\n",
      "Model and parameters:  DBSCAN(eps=7.0, metric='cityblock', min_samples=3)\n",
      "Homogeneity Score         :  0.9391266682415782\n",
      "Completeness Score        :  0.8025295487836166\n",
      "V-Measure Score           :  0.865471490811062\n",
      "Adjusted Rand Score       :  0.9200421180085234\n",
      "Adjusted Mutual Info Score:  0.8642565660656621\n",
      "Calinski Harabasz Score:   538.0611389718482\n",
      "Silhouette Score          :  0.6183426738336383\n",
      "****************************** output33 ******************************\n",
      "Model and parameters:  DBSCAN(eps=7.0, metric='cityblock', min_samples=4)\n",
      "Homogeneity Score         :  0.935194559865768\n",
      "Completeness Score        :  0.8019252583091074\n",
      "V-Measure Score           :  0.8634477957629635\n",
      "Adjusted Rand Score       :  0.9152035408014159\n",
      "Adjusted Mutual Info Score:  0.8623891187518747\n",
      "Calinski Harabasz Score:   623.2577836685018\n",
      "Silhouette Score          :  0.6646081612057977\n",
      "****************************** output34 ******************************\n",
      "Model and parameters:  DBSCAN(eps=7.0, metric='cityblock')\n",
      "Homogeneity Score         :  0.935194559865768\n",
      "Completeness Score        :  0.8019252583091074\n",
      "V-Measure Score           :  0.8634477957629635\n",
      "Adjusted Rand Score       :  0.9152035408014159\n",
      "Adjusted Mutual Info Score:  0.8623891187518747\n",
      "Calinski Harabasz Score:   623.2577836685018\n",
      "Silhouette Score          :  0.6646081612057977\n",
      "****************************** output35 ******************************\n",
      "Model and parameters:  DBSCAN(eps=7.0, metric='cityblock', min_samples=6)\n",
      "Homogeneity Score         :  0.9289351264035925\n",
      "Completeness Score        :  0.7940538744781042\n",
      "V-Measure Score           :  0.8562150261924124\n",
      "Adjusted Rand Score       :  0.9109021378040034\n",
      "Adjusted Mutual Info Score:  0.8551027373983752\n",
      "Calinski Harabasz Score:   616.4415239357917\n",
      "Silhouette Score          :  0.6622924213193557\n",
      "****************************** output36 ******************************\n",
      "Model and parameters:  DBSCAN(eps=7.0, min_samples=3)\n",
      "Homogeneity Score         :  0.7531795021371859\n",
      "Completeness Score        :  0.7602055054647493\n",
      "V-Measure Score           :  0.7566761944274405\n",
      "Adjusted Rand Score       :  0.7806796828076088\n",
      "Adjusted Mutual Info Score:  0.7535348518585404\n",
      "Calinski Harabasz Score:   346.40778628655517\n",
      "Silhouette Score          :  0.5633542267527026\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****************************** output37 ******************************\n",
      "Model and parameters:  DBSCAN(eps=7.0, min_samples=4)\n",
      "Homogeneity Score         :  0.7459593034267056\n",
      "Completeness Score        :  0.7711103931557741\n",
      "V-Measure Score           :  0.7583263617213793\n",
      "Adjusted Rand Score       :  0.7805158828853448\n",
      "Adjusted Mutual Info Score:  0.7562092660598247\n",
      "Calinski Harabasz Score:   456.56903218298436\n",
      "Silhouette Score          :  0.5886051545298231\n",
      "****************************** output38 ******************************\n",
      "Model and parameters:  DBSCAN(eps=7.0)\n",
      "Homogeneity Score         :  0.7453677822483761\n",
      "Completeness Score        :  0.7710825408866288\n",
      "V-Measure Score           :  0.758007136353707\n",
      "Adjusted Rand Score       :  0.7804704239687634\n",
      "Adjusted Mutual Info Score:  0.7558871528842975\n",
      "Calinski Harabasz Score:   452.7822776418443\n",
      "Silhouette Score          :  0.5867075480417666\n",
      "****************************** output39 ******************************\n",
      "Model and parameters:  DBSCAN(eps=7.0, min_samples=6)\n",
      "Homogeneity Score         :  0.7453677822483761\n",
      "Completeness Score        :  0.7710825408866288\n",
      "V-Measure Score           :  0.758007136353707\n",
      "Adjusted Rand Score       :  0.7804704239687634\n",
      "Adjusted Mutual Info Score:  0.7558871528842975\n",
      "Calinski Harabasz Score:   452.7822776418443\n",
      "Silhouette Score          :  0.5867075480417666\n"
     ]
    }
   ],
   "source": [
    "# dbscan test result\n",
    "for i in range(len(dbscan.scores)):\n",
    "    get_marks(dbscan.models[i], data=data, name=\"output\"+ str(i), db=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DBSCAN(eps=5.5, min_samples=6)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# best model\n",
    "dbscan.best_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8740224643320074"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The best evaluation value\n",
    "dbscan.best_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test batch search, the data length of each key in the hyperparameter dictionary must be the same\n",
    "eps = np.linspace(1,5,5)\n",
    "min_samples = list(range(2,7))\n",
    "dbscan_dict = {'eps': eps,\n",
    "        'min_samples': min_samples}\n",
    "dbscan.DBSCANBatchSearch(data,dbscan_dict,labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****************************** output0 ******************************\n",
      "Model and parameters:  DBSCAN(eps=1.0, min_samples=2.0)\n",
      "Homogeneity Score         :  0.8770368209088626\n",
      "Completeness Score        :  0.4622261281259435\n",
      "V-Measure Score           :  0.6053916958499453\n",
      "Adjusted Rand Score       :  0.6630097832259544\n",
      "Adjusted Mutual Info Score:  0.5842740257783278\n",
      "Calinski Harabasz Score:   61.606724855054516\n",
      "Silhouette Score          :  0.3757014609497442\n",
      "****************************** output1 ******************************\n",
      "Model and parameters:  DBSCAN(eps=2.0, min_samples=3.0)\n",
      "Homogeneity Score         :  0.908487822732969\n",
      "Completeness Score        :  0.6219174138132438\n",
      "V-Measure Score           :  0.7383722738299081\n",
      "Adjusted Rand Score       :  0.7637790030973867\n",
      "Adjusted Mutual Info Score:  0.7345097943453835\n",
      "Calinski Harabasz Score:   247.91817335013056\n",
      "Silhouette Score          :  0.5770044753331307\n",
      "****************************** output2 ******************************\n",
      "Model and parameters:  DBSCAN(eps=3.0, min_samples=4.0)\n",
      "Homogeneity Score         :  0.9255418652414475\n",
      "Completeness Score        :  0.7705044792277236\n",
      "V-Measure Score           :  0.8409371067092088\n",
      "Adjusted Rand Score       :  0.9028188292816695\n",
      "Adjusted Mutual Info Score:  0.8393102300580307\n",
      "Calinski Harabasz Score:   470.38214936357383\n",
      "Silhouette Score          :  0.6156277076946987\n",
      "****************************** output3 ******************************\n",
      "Model and parameters:  DBSCAN(eps=4.0, min_samples=5.0)\n",
      "Homogeneity Score         :  0.937012852318852\n",
      "Completeness Score        :  0.8050738428791314\n",
      "V-Measure Score           :  0.86604706863655\n",
      "Adjusted Rand Score       :  0.91727810224893\n",
      "Adjusted Mutual Info Score:  0.8650070751567023\n",
      "Calinski Harabasz Score:   624.0787278689478\n",
      "Silhouette Score          :  0.6662052248892947\n",
      "****************************** output4 ******************************\n",
      "Model and parameters:  DBSCAN(eps=5.0, min_samples=6.0)\n",
      "Homogeneity Score         :  0.941633320386537\n",
      "Completeness Score        :  0.8133705416579753\n",
      "V-Measure Score           :  0.8728149497674096\n",
      "Adjusted Rand Score       :  0.9227256894193715\n",
      "Adjusted Mutual Info Score:  0.8718236814427897\n",
      "Calinski Harabasz Score:   626.3421548945125\n",
      "Silhouette Score          :  0.6682063636077944\n"
     ]
    }
   ],
   "source": [
    "# dbscan test result\n",
    "for i in range(len(dbscan.scores)):\n",
    "    get_marks(dbscan.models[i], data=data, name=\"output\"+ str(i), db=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DBSCAN(eps=5.0, min_samples=6.0)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# the best model\n",
    "dbscan.best_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9227256894193715"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Best evaluation value\n",
    "dbscan.best_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
